<?php
use yii\helpers\Url;
?>
<div class="layuiadmin-form-admin">
    <form action="" method="post" class="layui-form layui-form-pane" style="padding:20px;">
        <div class="layui-form-item">
            <label for="name" class="layui-form-label">
                <span class="x-red">*</span>角色名
            </label>
            <div class="layui-input-inline">
                <input type="hidden" name="id" value="<?= $model->id ?>">
                <input type="hidden" name="<?=Yii::$app->request->csrfParam?>" value="<?=Yii::$app->request->csrfToken?>">
                <input type="text" id="name" name="name" required="" lay-verify="required"
                       autocomplete="off" class="layui-input" value="<?= $model->name ?>">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label for="description" class="layui-form-label">
                角色描述
            </label>
            <div class="layui-input-block">
                <textarea placeholder="请输入内容" id="description" lay-verify="required" name="description" class="layui-textarea" style="min-height: 50px"><?= $model->description ?></textarea>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">
                拥有权限
            </label>
            <table  class="layui-table layui-input-block">
                <tbody>
                <?php foreach ($menu as $list): ?>
                    <tr>
                        <td style="width: 180px" class="menu">
                            <input type="checkbox" name="menu[]" lay-skin="primary" lay-filter="father" title="<?= $list['route_name'] ?>">
                        </td>
                        <td>
                            <div class="layui-input-block">
                                <?php foreach ($list['route'] as $item): ?>
                                    <input name="ids[]" lay-skin="primary" type="checkbox" value="<?= $item['id'] ?>" title="<?= $item['route_name'] ?>"
                                    <?= in_array($item['id'], json_decode($model->jurisdiction)) ? 'checked' : '' ?>>
                                <?php endforeach; ?>
                            </div>
                        </td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
        <div class="layui-form-item">
            <button class="layui-btn" lay-submit="" lay-filter="update">更新</button>
        </div>
    </form>
</div>

<?php $this->beginBlock('footer') ?>
<script>
    layui.use(['form','layer'], function(){
        $ = layui.jquery;
        var form = layui.form, layer = layui.layer;

        form.on('checkbox(father)', function(data){
            if(data.elem.checked){
                $(data.elem).parent().siblings('td').find('input').prop("checked", true);
                form.render();
            }else{
                $(data.elem).parent().siblings('td').find('input').prop("checked", false);
                form.render();
            }
        });

        //监听提交
        form.on('submit(update)', function(data){
            //layer.load(3);
            $.post('<?= Url::to(['role/edit']) ?>', data.field, function (res) {
                console.log(res);
                layer.closeAll();
                if (res.status === 200) {
                    layer.msg(res.msg, {icon: 1,time: 1500}, function () {
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(index);
                        parent.location.reload();
                    })
                } else {
                    layer.msg(res.msg, {icon: 2, time: 1500})
                }
            }, 'json');
            return false;
        });

    });
</script>
<?php $this->endBlock() ?>
